//
//  ZYDownloadTask.m
//  ZYWebImage
//
//  Created by wangzhipan on 2025/3/14.
//

#import "ZYDownloadTask.h"
#import "ZYImageOperation.h"
#import "ZYImageOperationToken.h"

@interface ZYDownloadTask()
@property (nonatomic, assign, readwrite, getter=isCancelled) BOOL cancelled;
@property (nonatomic, weak) ZYImageOperation *downloadOperation;
@property (nonatomic, weak) ZYImageOperationToken *cancelToken;
@end

@implementation ZYDownloadTask
- (instancetype)initWithOperation:(ZYImageOperation *)downloadOperation
                            token:(ZYImageOperationToken *)token
{
    if (self = [super init]) {
        self.downloadOperation = downloadOperation;
        self.cancelToken = token;
        self.cancelled = NO;
    }
    return self;
}

- (void)cancel
{
    @synchronized (self) {
        if (self.isCancelled) {
            return;
        }
        self.cancelled = YES;
        [self.downloadOperation cancelSingle:self.cancelToken];
        self.cancelToken = nil;
    }
}

@end
